<?php
/**
 * Description of ViewGenerator
 *
 * @author BloumGenerator
 */
abstract class ViewGenerator {
    
    protected $tableMeta;
    protected $viewParams;
    protected $parentTbMeta;
    protected $templateView;
    protected $attPK;

    function __construct($tableMeta, $viewParams, $templateView) {
        $this->tableMeta = $tableMeta;
        $this->viewParams = $viewParams;
        $this->templateView = $templateView;
        $this->parentTbMeta = null;

        if(!file_exists($templateView))
            throw new Exception ("Template Not Found!!!");

        $this->templateView = file_get_contents($templateView);
        $this->searchPK();
        $this->searchParent();

    }

    private function searchPK(){
        $numExt = 0;
        foreach ($this->tableMeta->getFKReferences() as $fk) {
            if($fk->isExtends()){
                $numExt++;
                $this->parentTbMeta = $fk->getTableReference();
            }
        }
        if($numExt > 1){
            throw new Exception("Erro, Herança Multipla Não Suportado!!!");
        }
    }

    private function searchParent(){
        foreach ($this->tableMeta->getAttributes() as $att) {
            if($att->isPrimaryKey()){
                $this->attPK[$att->getName()] = $att;
            }
        }
    }

    public function generateNames(){
        $this->templateView = str_replace(Constantes::$TPL_BEAN_NAME_UPPER, ucfirst($this->tableMeta->getName()), $this->templateView);
        $this->templateView = str_replace(Constantes::$TPL_BEAN_NAME_LOWER, lcfirst($this->tableMeta->getName()), $this->templateView);
        $this->templateView = str_replace(Constantes::$TPL_BEAN_NAME_TO_UPPER, strtoupper($this->tableMeta->getName()), $this->templateView);
    }

    public function generatePkUrlParam(){
        $tableName = $this->tableMeta->getName();
        $pkUrlParam = "";

        $attrsKeys = array_keys($this->attPK);

        for ($i = 0; $i < count($attrsKeys); $i++) {
            $att = $this->attPK[$attrsKeys[$i]];
            $name = $att->getName();

            $pkUrlParam .= $name."=$".$name;

            if($i < ( count($this->attPK)-1 )){
                $pkUrlParam .= "&";
            }
        }

        $this->templateView = str_replace(Constantes::$TPL_VIEW_PK_URL_PARAM, $pkUrlParam, $this->templateView);
    }

    public function deleteCode($begin, $end){
        $beginCode = stripos($this->templateView, $begin);
        $endCode = stripos($this->templateView, $end)+strlen($end);

        $part1 = substr($this->templateView, 0, $beginCode);
        $part2 = substr($this->templateView, $endCode);

        $this->templateView = $part1.$part2;
    }
    
    public function generateFile($name){
        $dirView = "temp_gen/".$this->viewParams[Constantes::$PARAM_PROJECT_NAME]."/view/".$this->tableMeta->getName()."/";

        if(!file_exists($dirView))
            mkdir ($dirView, 0755,true);

        $fp = fopen($dirView.$name.".tpl", "w+");
        fwrite($fp, $this->templateView);
        fclose($fp);
    }

    public abstract function generate();
}
?>
